#include <stdio.h>
#include <string.h>
#include "MenuBackend.h"



/*
	This program demonstrates a menu modeled after the menubar in the Arduino IDE
	
   +root
	+file			+edit   +sketch			+tools			+help
	 +new			 +cut	 +verify (V)	 +autoformat	 +environment
	 +open
	 +examples
	  +ArduinoISP
*/


void menuUseEvent(MenuUseEvent used);
void menuChangeEvent(MenuChangeEvent changed);


//this controls the menu backend and the event generation
MenuBackend menu = MenuBackend( menuUseEvent, menuChangeEvent);
	//beneath is list of menu items needed to build the menu
	MenuItem miFile = MenuItem("File");
		MenuItem miNew = MenuItem("New");
		MenuItem miOpen = MenuItem("Open");
		MenuItem miExamples = MenuItem("Examples");
			MenuItem miArduinoISP = MenuItem("ArduinoISP");

	MenuItem miEdit = MenuItem("Edit");
		MenuItem miCut = MenuItem("Cut");

	MenuItem miSkecth = MenuItem("Sketch");
		MenuItem miVerify = MenuItem("Verify",'V');

	MenuItem miTools = MenuItem("Tools");
		MenuItem miAutoformat = MenuItem("Autoformat");

	MenuItem miHelp = MenuItem("Help");
		MenuItem miEnvironment = MenuItem("Environment");

/*
	This is an important function
	Here all use events are handled
	
	This is where you define a behaviour for a menu item
*/
void menuUseEvent(MenuUseEvent used)
{
	printf( "Menu use ");
	printf( "%s\n", used.item.getName());
	if( !strcmp( used.item.getName(), "ArduinoISP") ) //comparison using a string literal
	{
		printf("menuUseEvent found ArduinoISP\n");
	}
	if (used.item == miVerify) //comparison agains a known item
	{
		printf("menuUseEvent found Verify (V)\n");
	}
}

/*
	This is an important function
	Here we get a notification whenever the user changes the menu
	That is, when the menu is navigated
*/
void menuChangeEvent(MenuChangeEvent changed)
{
	printf( "Menu change ");
	printf( "%s", changed.from.getName());
	printf( " ");
	printf( "%s\n", changed.to.getName());
}


//this function builds the menu and connects the correct items together
void menuSetup()
{
	printf( "Setting up menu...");
	//add the file menu to the menu root
	//when add is used, as opposed to addX as you see below, the item is added below the item it's added to
	menu.getRoot().add(miFile); 
	//add all items below File to the file menu item, 
	//notice the arduino isp item is added _to the right_ of the examples item
	miFile.add(miNew).add(miOpen).add(miExamples).addRight(miArduinoISP);
	//because edit item is to the right of the file item, we use the addRight method when inserting this item
	//then we add the cut item, because it is below the edit
	miFile.addRight(miEdit).add(miCut);
	miEdit.addRight(miSkecth).add(miVerify);
	miSkecth.addRight(miTools).add(miAutoformat);
	miTools.addRight(miHelp).add(miEnvironment);
}

int getChar()
{
	char ch;
	printf( "Pulsa un caracter:");
	scanf( "%ch", &ch);
	printf( "\n");
	return ch;
}

void setup()
{
	//Serial.begin(9600);
	
	menuSetup();
	printf( "Starting navigation (see source for description):\n");
/*
	menu.moveDown();  //move to file
	menu.moveDown();  //move to new
	menu.moveDown();  //move to open
	menu.moveDown();  //move to examples
	menu.moveRight(); //move to arduinoisp
	menu.use();       //use arduniisp
	menu.moveLeft();  //move to examples
	menu.moveUp();    //move to open
	menu.moveUp();    //move to new
	menu.moveUp();    //move to file
	menu.moveRight(); //move to edit
	menu.moveRight(); //move to sketch
	menu.moveDown();  //move to verify
	menu.use();       //use verify
	menu.moveBack();  //move back to sketch
	menu.moveBack();  //move back to edit
	menu.moveBack();  //move back to file
	menu.moveBack();  //move back to new
	menu.use();       //use new
*/
//	menu.use('V');    //use verify based on its shortkey 'V'

//	printf( "%d\n", getChar());
}

void loop()
{
	char ch = getChar();
	if( ch == 'q') {
		menu.moveUp();
	}
	else if( ch == 'a') {
		menu.moveDown();
	}
	else if( ch == 'o') {
		menu.moveLeft();
	}
	else if( ch == 'p') {
		menu.moveRight();
	}
}	



int main( int argc, char** argv)
{
	setup( );
	while( 1) {
		loop( );
	}
	return 0;
}
